package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    //地址管理
    @Override
    public List<Address> findAddress() {
        //构建条件
        LambdaQueryWrapper<Address> aa=new LambdaQueryWrapper<>();
        aa.eq(Address::getUserId, UserHolder.get().getId());
        //调用mapper
        List<Address> addresses = addressMapper.selectList(aa);
        return addresses;
    }
    //添加地址
    @Override
    public void addAddress(Address address) {
        //补全信息
        address.setUserId(UserHolder.get().getId());
        address.setIsDefault(0);//默认地址
        address.setCreateTime(new Date());
        address.setCreateUser(UserHolder.get().getId());
        address.setUpdateTime(new Date());
        address.setUpdateUser(UserHolder.get().getId());
        // 2.调用mapper新增
        addressMapper.insert(address);
    }
    //修改回显地址
    @Override
    public Address findUpdate(Long id) {
        //构建条件
        LambdaQueryWrapper<Address> qw=new LambdaQueryWrapper<>();
        qw.eq(Address::getId,id);
        Address address = addressMapper.selectOne(qw);
        return address;
    }
    //修改地址
    @Override
    public void update(Address address) {
        //构建条件
        LambdaQueryWrapper<Address> qq=new LambdaQueryWrapper<>();
        qq.eq(Address::getId,address.getId());
        addressMapper.update(address,qq);
    }
    //设置默认地址
    @Override
    public void setDefault(Long id) {
        // 1-1 准备修改的内容
        Address address1 = new Address(); // set is_default =0
        address1.setIsDefault(0);
        address1.setCreateTime(new Date());
        address1.setCreateUser(UserHolder.get().getId());
        address1.setUpdateTime(new Date());
        address1.setUpdateUser(UserHolder.get().getId());
        //构建条件
        LambdaQueryWrapper<Address> ww=new LambdaQueryWrapper<>();
        ww.eq(Address::getUserId,UserHolder.get().getId());
        //构建条件
        addressMapper.update(address1,ww);
        // 2.将当前前端传递地址id设置为默认收货地址 update address_book set is_default =1 where id = ?
        Address address2 = new Address();
        address2.setIsDefault(1);
        address2.setCreateTime(new Date());
        address2.setCreateUser(UserHolder.get().getId());
        address2.setUpdateTime(new Date());
        address2.setUpdateUser(UserHolder.get().getId());
        address2.setId(id);//====================
        addressMapper.updateById(address2);

    }
    //查询默认地址
    @Override
    public Address findDefault() {
        //构建条件
        LambdaQueryWrapper<Address> aqw=new LambdaQueryWrapper<>();
        aqw.eq(Address::getIsDefault,1);
        aqw.eq(Address::getUserId,UserHolder.get().getId());
        Address address = addressMapper.selectOne(aqw);
        return address;

    }


}
